package club.zhilin.guli.service.statistics.service.impl;

import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.statistics.domain.Daily;
import club.zhilin.guli.service.statistics.feign.IEduCourseService;
import club.zhilin.guli.service.statistics.feign.IUserCenterMemberService;
import club.zhilin.guli.service.statistics.mapper.DailyMapper;
import club.zhilin.guli.service.statistics.service.IDailyService;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zhilin
 * @since 2022-03-05 23:19:33
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements IDailyService {

    private final IUserCenterMemberService iUserCenterMemberService;
    private final IEduCourseService iEduCourseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createStatisticsByDate(String date) {
        // 远程获取注册用户数量
        baseMapper.delete(new LambdaQueryWrapper<Daily>().eq(Daily::getDateCalculated, date));
        CommonResult<Integer> registerResult = iUserCenterMemberService.countRegister(date);
        CommonResult<Integer> loginResult = iUserCenterMemberService.countLogin(date);
        CommonResult<Integer> courseCountResult = iEduCourseService.coursePublishCount(date);
        Daily daily = new Daily();
        daily.setCourseNum(courseCountResult.getData());
        daily.setLoginNum(loginResult.getData());
        daily.setRegisterNum(registerResult.getData());
        daily.setDateCalculated(date);
        daily.setVideoViewNum(Integer.valueOf(RandomUtil.randomNumbers(3)));
        this.save(daily);
    }

    @Override
    public Map<String, Map<String, Object>> selectCharts(String beginTime, String endTime) {
        Map<String, Object> registerNum = this.selectChartData(beginTime, endTime, "register_num");
        Map<String, Object> loginNum = this.selectChartData(beginTime, endTime, "login_num");
        Map<String, Object> videoViewNum = this.selectChartData(beginTime, endTime, "video_view_num");
        Map<String, Object> courseNum = this.selectChartData(beginTime, endTime, "course_num");
        Map<String, Map<String, Object>> map = new HashMap<>(4);
        map.put("registerNum", registerNum);
        map.put("loginNum", loginNum);
        map.put("videoViewNum", videoViewNum);
        map.put("courseNum", courseNum);
        return map;
    }

    /**
     * 根据时间和要查询的列查询数据
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param type      要查询的列名
     * @return map
     */
    private Map<String, Object> selectChartData(String beginTime, String endTime, String type) {
        Map<String, Object> map = new HashMap<>(2);
        // 日期列表
        List<String> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        List<Map<String, Object>> maps = baseMapper.selectMaps(new QueryWrapper<Daily>().select("date_calculated", type).between("date_calculated", beginTime, endTime));
        maps.forEach(item -> {
            String calculated = (String) item.get("date_calculated");
            xList.add(calculated);
            Integer count = (Integer) item.get(type);
            yList.add(count);
        });
        map.put("xData", xList);
        map.put("yData", yList);
        return map;
    }
}
